﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

namespace WebSite.admin.user
{
    public partial class userlist : BaseAdmin
    {


        // Methods
        private void bind()
        {
            if (Request.QueryString["action"] == "del")
            {
                string sSql = "update DB_Adminlogin set IsDeleted=1 where Adminid = " + Request.QueryString["uid"];
                SQLAccess.DoSql(sSql);
                base.Response.Write("<script language='javascript'>;alert('删除成功');window.location.href='userlist.aspx';</script>");
                base.Response.End();
            }

            string sql = "select a.*,isnull((select [name] from tbCompany  where id = ca.CompanyId),a.AdminName) as CompanyName,ca.CompanyId "
                       +" from DB_Adminlogin a left join tbCompanyAdmin ca on a.AdminId = ca.AdminId  "
                        + " where 1=1 and isnull(IsDeleted,0) = 0  ";

            if (this.ddlUserType.SelectedItem.Value == "0")
            {
                sql = sql + " and isnull(a.AdminTypeId,0) = 0 and  a.AdminId not  in (select AdminId from tbCompanyAdmin)";
            }else if(this.ddlUserType.SelectedItem.Value == "1")
            {
                sql = sql + " and isnull(a.AdminTypeId,0) = 0  and a.AdminId in (select AdminId from tbCompanyAdmin)";
            }
            else if (this.ddlUserType.SelectedItem.Value == "2")//业务员
            {
                sql = sql + " and isnull(a.AdminTypeId,0) = 1 and isnull(ca.AdminTypeId,0) = 1  ";
            }


            sql = sql + " order by Companyname ";
            DataTable dtSource = null;
            DataSet ds = SQLAccess.GreatDs(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                dtSource = ds.Tables[0];
            }

            dtSource.DefaultView.RowFilter = string.Format(" AdminUser like '%{0}%' or CompanyName like '%{0}%'", this.tbxKeywords.Text.Trim());
            this.GridView1.DataSource = dtSource.DefaultView;
            this.GridView1.DataBind();

            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + dtSource == null ? "0" : dtSource.DefaultView.Count.ToString() + "个记录";
            if (dtSource == null || dtSource.DefaultView.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.bind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.bind();
        }





        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            this.bind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#efefef'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.AdminList);
       
                 this.bind();
                  
            }
        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.bind();
        }


    }
}